package code.ws_common_net.code
{
	import code.ws_common_interface.support.IDataPacket;
	import code.ws_common_net.utils.ByteBuffer;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	public class ReceivedPacket extends ByteBuffer implements IDataPacket
	{
		private var _handlerMap:Dictionary=null;
		public var id:int;
		public function ReceivedPacket(data:ByteArray=null)
		{
			super(data);
		}
		/**读取模块id*/
		public function bytesRead():void
		{
			id=this.readInt();
		}
		/**独立子模块*/
		public function addHandler(hid:int,handler:ResponseHandler):void
		{
			if(_handlerMap==null)
			{
				_handlerMap=new Dictionary();
			}
			_handlerMap[hid]=handler;
		}
		/**独立子模块执行*/
		protected function doResponse():void
		{
			if(_handlerMap==null)return ;
			var handler:ResponseHandler=_handlerMap[this.id];
			if(handler)
				handler.handler(this);
		}
		/**各个模块执行方法*/
		public function executed():void{}
		
		/**模块执行过程*/
		public function hander():void
		{
			bytesRead();
			
			executed();
			
			doResponse();
			
			clear();
		}
		
	}
}